﻿using ClientServer.Common;
using ConsoleApp2;
using log4net;
using server.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace server
{
    public class TestData
    {
        private static ILog logger = log4net.LogManager.GetLogger("server.TestData");

        public static string time;
        public static byte[]  GetData()
        {
            string firstData = "SL#0";
            byte[] first = ConverUtility.StringToBytes(firstData);
            List<byte> list = new List<byte>();
            list.AddRange(first);

            //当前的秒
            uint current_time_seconds = _GetTimeStamp();
            byte[] time_byte_seconds = BitConverter.GetBytes(current_time_seconds);
            Array.Reverse(time_byte_seconds);
           // time_byte_seconds.Reverse();
            list.AddRange(time_byte_seconds);

            long _totaltime = GetTimeStamp();

            double millin = _totaltime / 1000.0d;
            double decimalPart = millin - Math.Floor(millin);
            uint _millin = (uint)(decimalPart * 1000);
            time = TimeSpanConverDateTime(current_time_seconds) + "." + _millin;
            Console.WriteLine("time={0},_millin={1}", TimeSpanConverDateTime(current_time_seconds), _millin);
            byte[] millin_time_byte_seconds = BitConverter.GetBytes(_millin);
            Array.Reverse(millin_time_byte_seconds);
            list.AddRange(millin_time_byte_seconds);
            //list.Add(0x67);
            //list.Add(0x6e);
            //list.Add(0x52);
            //list.Add(0xaf);
            //list.Add(0x22);
            //list.Add(0xdf);
            //list.Add(0x33);
            //list.Add(0x48);

            int has = RandomUtils.NextInt(5,15);
            byte[] hasbyte = BitConverter.GetBytes(has);
            Array.Reverse(hasbyte);
            list.AddRange(hasbyte);

            float value1 = (float)RandomUtils.NextDouble(0.1d, 23.0d,3);
            byte[] value1_byte = BitConverter.GetBytes(value1);
            Array.Reverse(value1_byte);
            list.AddRange(value1_byte);

            float value2 = (float)RandomUtils.NextDouble(0.1d, 20.0d,3);
            byte[] value2_byte = BitConverter.GetBytes(value2);
            Array.Reverse(value2_byte);
            list.AddRange(value2_byte);

            float value3 = (float)RandomUtils.NextDouble(0.2d,35.0d,3);
            byte[] value3_byte = BitConverter.GetBytes(value3);
            Array.Reverse(value3_byte);
            list.AddRange(value3_byte);

            float value4 = (float)RandomUtils.NextDouble(0.7d, 15d,3);
            byte[] value4_byte = BitConverter.GetBytes(value4);
            Array.Reverse(value4_byte);
            list.AddRange(value4_byte);
            list.Add(0x0A);
            list.Add(0x0D);
            return list.ToArray();
        }
        public static long GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalMilliseconds);
        }

        public static uint _GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return (uint)Convert.ToInt64(ts.TotalSeconds);
        }

        public static string TimeSpanConverDateTime(long timestamp)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));//当地时区
            DateTime time = startTime.AddSeconds(timestamp);
            return time.ToString("HH:mm:ss");
        }


        public static byte[] GetDataByIndex(int i, uint current_time_seconds)
        {
            string firstData = "SL#0";
            byte[] first = ConverUtility.StringToBytes(firstData);
            List<byte> list = new List<byte>();
            list.AddRange(first);

            //前四个字节为秒，后四个字节为毫秒
            //当前的秒,
           // uint current_time_seconds = _GetTimeStamp();
            byte[] time_byte_seconds = BitConverter.GetBytes(current_time_seconds);
            Array.Reverse(time_byte_seconds);
            // time_byte_seconds.Reverse();
            list.AddRange(time_byte_seconds);

            
            //long _totaltime = GetTimeStamp();.

            //double millin = _totaltime / 1000.0d;
            //double decimalPart = millin - Math.Floor(millin);
            uint _millin = (uint)(i);
            time = TimeSpanConverDateTime(current_time_seconds) + "." + _millin;
            logger.Info(time);
           // Console.WriteLine("time={0},_millin={1}", TimeSpanConverDateTime(current_time_seconds), _millin);
            byte[] millin_time_byte_seconds = BitConverter.GetBytes(_millin);
            Array.Reverse(millin_time_byte_seconds);
            list.AddRange(millin_time_byte_seconds);

            //list.Add(0x67);
            //list.Add(0x6e);
            //list.Add(0x52);
            //list.Add(0xaf);
            //list.Add(0x22);
            //list.Add(0xdf);
            //list.Add(0x33);
            //list.Add(0x48);

            //DasDigitalOut
            int has = RandomUtils.NextInt(5, 15);
            byte[] hasbyte = BitConverter.GetBytes(has);
            Array.Reverse(hasbyte);
            list.AddRange(hasbyte);

            for(int k=0;k<100;k++)
            {
                float value1 = (float)RandomUtils.NextDouble(0.1d, 23.0d, 3);
                byte[] value1_byte = BitConverter.GetBytes(value1);
                Array.Reverse(value1_byte);
                list.AddRange(value1_byte);
            }
           
            //float value2 = (float)RandomUtils.NextDouble(0.1d, 20.0d, 3);
            //byte[] value2_byte = BitConverter.GetBytes(value2);
            //Array.Reverse(value2_byte);
            //list.AddRange(value2_byte);

            //float value3 = (float)RandomUtils.NextDouble(0.2d, 35.0d, 3);
            //byte[] value3_byte = BitConverter.GetBytes(value3);
            //Array.Reverse(value3_byte);
            //list.AddRange(value3_byte);

            //float value4 = (float)RandomUtils.NextDouble(0.7d, 15d, 3);
            //byte[] value4_byte = BitConverter.GetBytes(value4);
            //Array.Reverse(value4_byte);
            //list.AddRange(value4_byte);



            list.Add(0x0A);
            list.Add(0x0D);
            return list.ToArray();
        }

    }

    
}
